#!/usr/bin/env python
"""

    thousands_separator.py

"""

################################################################################
#
#   thousands_separator
#
#
#   Copyright (C) 2009 Leo Goodstadt
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; version 2
#   of the License
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#################################################################################

import sys, os
from collections import defaultdict
import re

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Functions        


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
def _sub_commify(s):

    r = []
    for i, c in enumerate(reversed(s)):
        if i and (not (i % 3)):
            r.insert(0, ',')
        r.insert(0, c)
    return ''.join(r)

    
re_digits_nondigits = re.compile(r'\d+|\D+')
def commify(format, value):
    """
    
    """
    parts = re_digits_nondigits.findall(format % (value,))
    for i in xrange(len(parts)):
        s = parts[i]
        if s.isdigit():
            parts[i] = _sub_commify(s)
            break
    return ''.join(parts)
    



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Testing


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
import unittest
class Test_thousands_separator(unittest.TestCase):

    #       self.assertEqual(self.seq, range(10))
    #       self.assert_(element in self.seq)
    #       self.assertRaises(ValueError, random.sample, self.seq, 20)



    def test_function(self):
        """
            test 
        """

        self.assertEqual(commify('%.4f', .1234), '0.1234')
        self.assertEqual(commify('%i', 100), '100')
        self.assertEqual(commify('%.4f', 234.5678), '234.5678')
        self.assertEqual(commify('$%.4f', 234.5678), '$234.5678')
        self.assertEqual(commify('%i', 1000), '1,000')
        self.assertEqual(commify('%.4f', 1234.5678), '1,234.5678')
        self.assertEqual(commify('$%.4f', 1234.5678), '$1,234.5678')
        self.assertEqual(commify('%i', 1000000), '1,000,000')
        self.assertEqual(commify('%.4f', 1234567.5678), '1,234,567.5678')
        self.assertEqual(commify('$%.4f', 1234567.5678), '$1,234,567.5678')
        self.assertEqual(commify('%i', -100), '-100')
        self.assertEqual(commify('%.4f', -234.5678), '-234.5678')
        self.assertEqual(commify('$%.4f', -234.5678), '$-234.5678')
        self.assertEqual(commify('%i', -1000), '-1,000')
        self.assertEqual(commify('%.4f', -1234.5678), '-1,234.5678')
        self.assertEqual(commify('$%.4f', -1234.5678), '$-1,234.5678')
        self.assertEqual(commify('%i', -1000000), '-1,000,000')
        self.assertEqual(commify('%.4f', -1234567.5678), '-1,234,567.5678')
        self.assertEqual(commify('$%.4f', -1234567.5678), '$-1,234,567.5678')

#
#   debug code not run if called as a module
#     
if __name__ == '__main__':
    if sys.argv.count("--debug"):
        sys.argv.remove("--debug")
    unittest.main()



    
